package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.entity.Employee;
import com.qfedu.hotelssm.service.intf.EmployeeService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author truman
 * @date 2022/8/28
 */
@RestController
@RequestMapping("/api/emp/")
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    /**
     * 新增api
     * @param employee
     * @return
     */
    @PostMapping("save.do")
    public R save(Employee employee) {
        return service.save(employee);
    }

    /**
     * 删除api
     * @param id
     * @return
     */
    @GetMapping("del.do")
    public R delete(int id) {
        return service.delete(id);
    }

    /**
     * 更新api
     * @param employee
     * @return
     */
    @PostMapping("update.do")
    public R update(Employee employee) {
        return service.update(employee);
    }
    /**
    分页查询api
     */
    @GetMapping("page.do")
    public PageBean select(String name,int page, int limit) {
        return service.selete(name, page, limit);
    }
    /**
     * 查询所有api
     */
    @GetMapping("all.do")
    public R all() {
        return service.all();
    }
    /**
     * 导出表格
     */
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        List<Employee> list = service.download();
        response.setHeader("content-disposition", "attachment;filename=employee"+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(),Employee.class).sheet("员工表").doWrite(list);
    }
    /**
     * 批量删除
     */
    @PostMapping("deletes.do")
    public R deletes(int[] id) {
        return service.deletes(id);
    }
    /**
     * 批量导入
     */
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
    /**
     * 男女比例人数
     */
    @GetMapping("countSex.do")
    public R countSex() {
        return service.countSex();
    }
    /**
     * 部门人数
     */
    @GetMapping("countDep.do")
    public R countDepartment() {
        return service.countDepartment();
    }
}
